package net.itgoo.parkingpay.ui.merchants;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import net.itgoo.parkingpay.R;
import net.itgoo.parkingpay.rest.model.ParkingMerchantModel;

import java.util.ArrayList;
import java.util.List;

public class ParkingMerchantsAdapter extends RecyclerView.Adapter<ParkingMerchantsAdapter.RecyclerHolder> {
    private Context mContext;
    private List<ParkingMerchantModel> mDataList;
    private OnItemClickListener mOnItemClickListener;

    public ParkingMerchantsAdapter(Context context) {
        this.mContext = context;
    }

    public void setOnItemClickListener(ParkingMerchantsAdapter.OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public void setData(List<ParkingMerchantModel> dataList) {
        if (null != dataList) {
            mDataList = dataList;
            notifyDataSetChanged();
        }
    }

    @Override
    public ParkingMerchantsAdapter.RecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.parking_item_merchants, parent, false);
        return new ParkingMerchantsAdapter.RecyclerHolder(view);
    }

    @Override
    public void onBindViewHolder(ParkingMerchantsAdapter.RecyclerHolder holder, final int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(v, position);
                }
            }
        });
        holder.titleTextView.setText(mDataList.get(position).company);
        holder.plateTextView.setText(mDataList.get(position).plate);
        holder.termTextView.setText(mDataList.get(position).term);
        if ("Month".equals(mDataList.get(position).cardType)) {
            holder.typeTextView.setVisibility(View.VISIBLE);
        } else {
            holder.typeTextView.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    class RecyclerHolder extends RecyclerView.ViewHolder {
        TextView titleTextView;
        TextView typeTextView;
        TextView plateTextView;
        TextView termTextView;
        private RecyclerHolder(View itemView) {
            super(itemView);
            titleTextView = itemView.findViewById(R.id.parking_item_merchants_title_tv);
            typeTextView = itemView.findViewById(R.id.parking_item_merchants_type_tv);
            plateTextView = itemView.findViewById(R.id.parking_item_merchants_plate_tv);
            termTextView = itemView.findViewById(R.id.parking_item_merchants_term_tv);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View v, int position);
    }
}
